//上传初始方法file为input file组件name,list_target为显示div的容器,max为可上传的图片数量.fileType为允许上传的类型.fileSizes为图片上传的最大值单位为K
function initFileInput(file,list_target, max,fileType,ext,fileSizes)
{
		var divListObj = new DivListObj(list_target,file,max,fileType,ext,fileSizes);
		divListObj.init(divListObj);
}

function checkImageSize(inputObj,fileSizes)
{
	 if(fileSizes == null){
	 	fileSizes = 500;
	 }
	 var result = true;
	 var img =  new Image();//document.createElement('img');
	 img.dynsrc = inputObj.value;
	 if(img.fileSize == -1 || img.fileSize> 1024*fileSizes){
	 	ErrorAlert('图片大小超过'+fileSizes+'K,请重新选择!');
		 result = false;
	 }
	return result;
}

function setFile(divListObj)
{
	var files = divListObj.getElementsByTagName('file');
}


function checkFileExt(inputObj,fileType,fileSizes)
{
    var allowImageExt="jpg|JPG|gif|GIF|jpeg|png|bmp|BMP";
	var allowFileExt="txt|doc|xls|csv";
	var ext = inputObj.value.substring(inputObj.value.lastIndexOf(".")+1).toLowerCase();
	if(fileType != null){
		allowImageExt = fileType;
	}
	if(allowImageExt.indexOf(ext) != -1 )
	{
		return checkImageSize(inputObj,fileSizes);
	}
	else
	{
		ErrorAlert("不支持"+ext+"文件类型上传!");
		return false;
	}
	return true;
}

//this method must set IE property
 function   getFileSize(inputObj){
      if(document.layers){
          if(navigator.javaEnabled()){
              var   file   =   new   java.io.File(inputObj.value);
              if(location.protocol.toLowerCase()   !=   'file:')
                  netscape.security.PrivilegeManager.enablePrivilege(
                  'UniversalFileRead'
                  );
              return   file.length();
          }
          else   return   -1;
      }
      else if(document.all){
          window.oldOnError   =   window.onerror;
          window.onerror   =   function (err){
              if(err.indexOf('utomation')!= -1){
                  alert('file   access   not   possible');
                  return   true;
              }
              else
                  return   false;
          };
          var   fso   =   new   ActiveXObject('Scripting.FileSystemObject');
          var   file   =   fso.GetFile(inputObj.value);
          window.onerror   =   window.oldOnError;
          return   file.Size;
      }
  }


/**
*  存放所有文件DIV容器.
*/
function DivListObj(selectDivId,inputFileId,maxSize,fileType,ext,fileSizes){
	  this.divListObj = document.getElementById(selectDivId);

	  this.currentInputFile = document.getElementById(inputFileId);

	  this.divListObj.currentCount = 0;

	  this.divListObj.maxSize = maxSize;

	  this.divListObj.fileType = fileType;

	  this.divListObj.fileSizes = fileSizes;

	  this.divListObj.ext = ext;
	  //初始化，当修改数据时，div列表可能会有些文件了，这里需要给添加到divListObj中
	  this.init = function(){
			//get div containter img count.exclude<script> element,this element contain in ImageUploadTag.java
			//var len = this.divListObj.children.length-1;
			var len = this.divListObj.getElementsByTagName("div").length;
			//alert(len);
			//alert(this.divListObj.innerHTML);
	    	this.divListObj.addDivObj  = this.addDivObj;

	   	    this.divListObj.deleteDivObj  = this.deleteDivObj ;

			this.divListObj.currentInputFile =this.currentInputFile;

			this.divListObj.currentInputFile.fileType = this.divListObj.fileType;

			this.divListObj.currentInputFile.ext = this.divListObj.ext;

	   	    this.currentInputFile._divListObj  = this.divListObj;

		    this.currentInputFile.onchange = function() {
		  	   this._divListObj.addDivObj(this);
		    }
			//if modify image obj,this len meaning to init obj count
			if(len>=1)
			{
				//获取img对象,img._divListObj.deleteDivObj(this);
				//alert(this.divListObj.getElementsByTagName('IMG').length);
				var imgs = this.divListObj.getElementsByTagName('IMG');
				for(var i=0;i<imgs.length;i++){
					imgs[i]._divListObj = this.divListObj;
					imgs[i].onclick = function(){
						this._divListObj.deleteDivObj(this);
					}
				}
				//alert(this.currentInputFile.id);
				//若以有文件,可重新设置当前值.
			    this.divListObj.currentCount = len;

				if(len == this.divListObj.maxSize ){
					this.currentInputFile.disabled = true;
				}
			}

	  }

	  //添加文件
	  this.addDivObj = function(inputObj){
	  	 if(checkFileExt(inputObj,fileType,fileSizes)){
	  	//创建div,a,input然后放到divListObj中
	  	//var aObj = new AObj().buildAObj();
		var imageObj = new ImageObj(inputObj._divListObj).buildImageObj();
		//alert(inputObj.value);
	  	var aObj = new AObj(inputObj).buildAObj();
	  	var divObj = new DivObj(inputObj,aObj,imageObj).buildDivObj();

	  	inputObj._divListObj.insertBefore(divObj);

	  	//新生成一个inputFile
		//modify by davide
		//alert(inputObj._divListObj.ext);
	  	var inputFile = new InputFile(inputObj._divListObj).buildInputFile(inputObj._divListObj.fileType,inputObj._divListObj.ext);
		//设置当前input file
		inputFile._divListObj.currentInputFile = inputFile;
		inputObj.parentNode.appendChild(inputFile);
	  	divObj.appendChild(inputObj);
		var files = inputObj._divListObj.getElementsByTagName("INPUT");
		//alert("ss"+files.length);
		if(files.length>0){
			var beforefile = files[files.length-1];
			//alert(beforefile.value);
			beforefile.style.display="none";
		}
		//alert(document.getElementById(selectDivId).innerHTML );
		inputObj._divListObj.currentCount++;
		if(inputFile._divListObj.currentCount == inputFile._divListObj.maxSize ){
			inputFile.disabled = true;
		}
		}else{
			var who2= inputObj.cloneNode(false);
			who2.onchange= inputObj.onchange;// events are not cloned
			inputObj.parentNode.replaceChild(who2,inputObj);
		}
	  }
	  //删除文件
	  this.deleteDivObj = function(imageObj){

	  	imageObj._divListObj.removeChild(imageObj.parentNode);
		var len = imageObj._divListObj.currentCount--;

		if(len -1 <=imageObj._divListObj.maxSize)
		{
			//this为container,imageObj._divListObj =  divName
			//alert(imageObj._divListObj.currentInputFile);
			imageObj._divListObj.currentInputFile.disabled = false;

		}
	  }

}

/**
*图片预览
*/
function preview(filePath){
	if(filePath==null || filePath==''){
		return false;
	}
	var url = encodeURI(encodeURI(PROJECT_NAME_PATH_MYCE + "/common_commonAction.do?method=initImagePreview&filePath=" + filePath));
	top.windowOpen(url,"layOutTrafficImage",'预览',600,490);
}

/**
* 单个超链接、图片DIV容器对象
*/
function DivObj(inputObj,aObject,imageObject){
	this.inputFile = inputObj; //文件上传控件

	this.aObj = aObject;   //文件超连接

	this.imageObj = imageObject; //删除图片对象

	 /**
	 *  创建一个DIV，里面有<a><img>用户现实图片文件连接、删除文件图片
	 */
	 this.buildDivObj = function(){
	 		 var newDivObj = document.createElement('div');
	 		 //newDivObj.className="background-color:blue";
			 //newDivObj.className = "inputdiv";
	 		 newDivObj.appendChild(this.aObj);
	 		 newDivObj.appendChild(this.imageObj);

	 		 return newDivObj;
		}
}

/**
*  文件输入框
*/
function InputFile(_divListObj){

	 this.divListObj = _divListObj;
	 /**
	 * 创建一个<intpu type="file" class="addFile" id="">
	 */
	 this.buildInputFile = function(fileType,ext){
    	var newInputFile = document.createElement('input');
		var index = this.divListObj.currentCount+1;
		//alert(this._divListObj.fileType);
		newInputFile.name = "file_"+index+"_"+ext;
		//alert(newInputFile.id);
    	newInputFile.type = 'file';
    	newInputFile.size = "1";
		newInputFile.style.width = "20px";

		if(fileType == "null")
    		newInputFile.className = "addfile";
		else
		    newInputFile.className = "addfile validate-file-"+fileType;
    	newInputFile.onchange = function(){
			this._divListObj.addDivObj(this);
		};
	  	newInputFile._divListObj  = this.divListObj;
    	return  newInputFile;
	 }
}

/**
*  文件名超链接
*/
function AObj(inputObj){
	/*
	* 创建文件名超连接
	*/
	this._inputObj = inputObj;
	this.buildAObj = function(){
		var newAObj = document.createElement('A');
		var url = this._inputObj.value;
		res = /\\+/g;
		str = url.replace(res,"\\\\");
		newAObj.href = "javascript:preview('"+str+"')";
		newAObj.innerHTML = this._inputObj.value.substring(this._inputObj.value.lastIndexOf("\\")+1)+ " ";;
		return newAObj;
	}
}

/**
* 删除图片对象
*/
function ImageObj(divListObj){
	this._divListObj = divListObj;
	 /**
	 * 创建删除图片IMG
	 */
	 this.buildImageObj = function () {
			var newImageObj = document.createElement('img');
			newImageObj.src = "images/ico_delete.gif";

			//删除方法
			newImageObj._divListObj  = this._divListObj;
      newImageObj.onclick = function (){
      	 this._divListObj.deleteDivObj(this);
      }
	 		return newImageObj;
	}
}

